package com.zige.zige.dialog;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.zige.zige.R;
import com.zige.zige.activity.PersonalInformationActivity;
import com.zige.zige.db.ProviceCityDao;
import com.zige.zige.pojo.Place;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fei on 2015/12/30.
 */
public class CitySelectDialog extends DialogFragment{
    private View view;
    private TextView tv_prompt_title,tv_prompt_content;
    private Button btn_ok,btn_canner_cf;

    private Spinner sp_province, sp_city;
    String province,city,myAddress;

    private ProviceCityDao dao;
    private final int QUREY_PROVINCE = 0;
    private final int QUREY_CITY = 1;

    private ArrayAdapter<String> provincesAdapter;
    private ArrayAdapter<String> citysAdapter;

    private List<Place> provinces;
    private List<String> provinceNames = new ArrayList<>();

    ;
    private List<Place> citys;
    private List<String> citynames = new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case QUREY_PROVINCE:
                    provinceNames.clear();
                    for (Place place : provinces) {
                        provinceNames.add(place.name);
                    }
                    provincesAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, provinceNames);
                    provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sp_province.setAdapter(provincesAdapter);
                    break;
                case QUREY_CITY:
                    citynames.clear();
                    for (Place place : citys) {
                        citynames.add(place.name);
                    }
                    if (citysAdapter == null) {
                        citysAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, citynames);
                        citysAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        sp_city.setAdapter(citysAdapter);
                    }
                    citysAdapter.notifyDataSetChanged();
                    break;

            }
        }
    };

    public interface CitySelectDialogListener
    {
        void OnCitySelectDialogListenerInputComplete(String address);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dao = new ProviceCityDao(getActivity());

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        view = inflater.inflate(R.layout.dialog_select_city, container);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        Window window = getDialog().getWindow();

        WindowManager.LayoutParams lp = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(lp);
        getDialog().show();
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().setCancelable(false);

        initView();
        findView();
        return  view;

    }

    private void initView(){
        tv_prompt_content = (TextView)view.findViewById(R.id.tv_prompt_content);
        tv_prompt_content.setText("选择所在城市");
        btn_ok = (Button)view.findViewById(R.id.btn_ok_cf);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                province = sp_province.getSelectedItem().toString();
                city = sp_city.getSelectedItem().toString();
                myAddress = province +"-"+ city;
                CitySelectDialogListener  listener = (CitySelectDialogListener) getActivity();
                listener.OnCitySelectDialogListenerInputComplete(myAddress);
                getDialog().dismiss();
            }
        });
        btn_canner_cf = (Button)view.findViewById(R.id.btn_canner_cf);
        btn_canner_cf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDialog().dismiss();
            }
        });
    }

    private void findView() {
        sp_province = (Spinner) view.findViewById(R.id.sp_myprovince);
        sp_province.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                if (citys != null)
                    citys.clear();
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        citys = dao.qureyAllCityByProvice(String.valueOf(position + 1));
                        handler.sendEmptyMessage(QUREY_CITY);
                    }
                }.start();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        sp_city = (Spinner) view.findViewById(R.id.sp_mycity);
        sp_city.setPrompt("市");
        setProvinceSpinner();
    }

    private void setProvinceSpinner() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                provinces = dao.qureyAllProvice();
                handler.sendEmptyMessage(QUREY_PROVINCE);
            }
        }.start();
    }




}
